// pages/user/authentication/authentication.js
const app = getApp();
let api = require('../../../utils/api.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['5年以下', '5', '6', '7', '8', '9', '10', '10年以上'],
    index: 0,
    driverAgeDialogShow: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

    this.getAuthenList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  //  onShareAppMessage: function() {

//  },

  bindPickerChange(e) {
    var that = this;
    var driverAge = that.data.driverAge;
    var token = wx.getStorageSync('TOKEN');
    var param = {
      "driverAge": driverAge
    };
    if (driverAge == 0) {
      wx.showToast({
        title: '请填写正确驾驶年龄',
        icon: 'none'
      })
      return;
    }
    api._put('/chauffeur/edit/driver/age', param, token).then(res => {
      var item = 'list.driverAge';
      if (res.code == 200) {
        that.setData({
          [item]: driverAge
        })

        that._cancelEvent();
      }
    })
  },
  /**
   * 获取认证状态
   */
  getAuthenList: function(e) {
    var that = this;
    var token = wx.getStorageSync('TOKEN');
    var showState = [];
    showState[-1] = '认证未通过';
    showState[0] = '认证审核中';
    showState[1] = '已认证';
    api._get('/chauffeur/myself/info', '', token).then(res => {
      if (res.code == 200) {
        var list = res.result;
        if (list.nameState != null) {
          list.showNameState = showState[list.nameState];
        } else {
          list.showNameState = '未认证';
        }

        if (list.licenseState != null) {
          list.showLicenseState = showState[list.licenseState];
        } else {
          list.showLicenseState = '未认证';
        }
        if (list.vehicleAuthState != null) {
          if (list.vehicleModel != null) {
            list.showVehicleModel = list.vehicleModel;
          } else {
            list.showVehicleModel = showState[list.vehicleAuthState];
          }

        } else {
          list.showVehicleModel = '未认证';

        }
        if (list.jobState != null) {
          list.showJobState = showState[list.jobState];
        } else {
          list.showJobState = '未认证';
        }

        if (list.vehicleNoIdState != null) {
          list.showVehicleNoIdState = showState[list.vehicleNoIdState];
        } else {
          list.showVehicleNoIdState = '未认证';
        }
        if (list.companyAuthState != null) {
          if (list.companyName == null) {
            list.showCompanyName = showState[list.companyAuthState];
          }
        } else {
          list.showCompanyName = '未认证';
        }

        that.setData({
          list: list
        })

      } else if (res.code > 400) {

        wx.navigateTo({
          url: '../../login/login',
        })
      } else {
        wx.showToast({
          title: '网络忙请重试',
          icon: 'none',
        })
      }
    })
  },

  /**
   * 跳转
   */
  navigator: function(e) {
    var that = this;
    var data = that.data.list;
    var state = e.currentTarget.dataset.state;
    if (state == 1 || state == 0) {
      return;
    } else {
      wx.navigateTo({
        url: e.currentTarget.dataset.url,
      })
    }
  },

  // 关闭弹框
  _cancelEvent: function(e) {
    this.setData({
      driverAgeDialogShow: false,
    });
  },

  // 编辑驾驶年龄
  editDriverAge: function() {
    this.setData({
      driverAgeDialogShow: true
    })
  },

  // 输入监听
  inputWacth: function(e) {
   // console.log(e);
    let item = e.currentTarget.dataset.model;
    this.setData({
      [item]: e.detail.value
    });
  },
})